﻿using AutoWelding.Models;

using MySql.Data.MySqlClient;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoWelding.DAL
{
    public class AlarmDAL
    {
        /// <summary>
        /// 条件查询报警信息
        /// </summary>
        /// <param name="alarmType"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="isHandled"></param>
        /// <returns></returns>
        public List<AlarmModel> GetAlarmList(string alarmType,DateTime startTime, DateTime endTime, int isHandled)
        {
            List<AlarmModel> list = new List<AlarmModel>();
            List<MySqlParameter> parameters = new List<MySqlParameter>();

            string selectSql = "select * from alarm_info where 1=1";
            if (!string.IsNullOrEmpty(alarmType))
            {
                selectSql += " and alarm_type = @alarmType";
                parameters.Add(new MySqlParameter("@alarmType", MySqlDbType.VarChar) { Value = alarmType });
            }
            if (isHandled != -1)
            {
                selectSql += " and is_handled = @isHandled";
                parameters.Add(new MySqlParameter("@isHandled", MySqlDbType.Int32) { Value = isHandled });
            }
            selectSql += " and create_time between @startTime and @endTime order by create_time DESC";
            parameters.Add(new MySqlParameter("@startTime", MySqlDbType.DateTime) { Value = startTime });
            parameters.Add(new MySqlParameter("@endTime", MySqlDbType.DateTime) { Value = endTime });
            //if (startTime != null)
            //{
            //    selectSql += " and CreateTime >= @startTime";
            //    parameters.Add(new MySqlParameter("@startTime", MySqlDbType.DateTime) { Value = startTime });
            //}
            //if (endTime != null)
            //{
            //    selectSql += " and CreateTime <= @endTime";
            //    parameters.Add(new MySqlParameter("@endTime", MySqlDbType.DateTime) { Value = endTime });
            //}

            Console.WriteLine("sql语句是" + selectSql);
            return MysqlHelp.ExecuteQuery<AlarmModel>(selectSql,
                reader => new AlarmModel
                {
                    Id = Convert.ToInt32(reader["id"]),
                    AlarmMessage = reader["alarm_message"].ToString(),
                    AlarmType = reader["alarm_type"].ToString(),
                    CreateTime = Convert.ToDateTime(reader["create_time"]),
                    //CreateTimeFormat = Convert.ToDateTime(reader["create_time"]).ToString("yyyy-MM-dd HH:mm:ss"),
                    HandleTime = Convert.ToDateTime(reader["handle_time"]),
                    //HandleTimeFormat = Convert.ToDateTime(reader["handle_time"]).ToString("yyyy-MM-dd HH:mm:ss"),
                    IsHandled = Convert.ToInt32(reader["is_handled"]),
                    Handler = reader["handler"].ToString(),
                    Remark = reader["remark"].ToString(),
                }
                , parameters.ToArray()
                );
        }
    }
}
